<?php
/**
 * 车商确认记录 放入 验车记录中去
 * @author angl
 */
die("exit");
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

//初始化redis
function init_redis()
{
    $redis = new Redis();
    $redis->connect(REDIS_HOST, REDIS_PORT);
    return $redis;
}

$db = init_db();

$sql = "SELECT o.order_id,o.dealer_confirm_service_id,cd.dealer_id,co.sign_date,o.last_time FROM au_order o 
        INNER JOIN au_cars as c ON c.car_id=o.car_id 
        INNER JOIN au_car_owner as co ON co.owner_id=c.owner_id 
        INNER JOIN au_dealer_auctioned as da ON da.order_id=o.order_id 
        INNER JOIN au_car_dealer as cd ON cd.dealer_id=da.dealer_id 
        WHERE o.status = '8' AND da.status = 0 AND o.dealer_confirm_status < '4'";

$list = $db->query($sql);
foreach($list as $item){
    $data = array();
    $order_data = array();
    $data['order_id'] = $item['order_id'];
    $data['dealer_id'] = $item['dealer_id'];
    $data['createtime'] = $item['sign_date'];
    $data['check_limit_time'] = date("Y-m-d H:i:s",strtotime($item['sign_date']." + 1 day"));
    if($item['dealer_confirm_service_id']>0){
        $data['emp_id'] = $item['dealer_confirm_service_id'];
        $data['status'] = 1;
        $order_data['check_car_status'] = 2;
    }else{
        $order_data['check_car_status'] = 1;//待验车
    }
    $db->startTransaction();
    $db->insert('car_dealer_check',$data);
    $db->where("order_id",$item['order_id'])->update("order",$order_data);

    $db->commit();
    logUtil("order_id :".$item['order_id']." 处理成功","INFO");
}
logUtil("车商待确认,跟踪中数据处理完成","INFO");

$log_list = $db->where("(result = ? or result = ?)",array(1,3))->where("createtime",'2015-12-10',">=")->get('dealer_confirm_log');
foreach ($log_list as $log){
    $data = array(
        'order_id'=>$log['order_id'],
        'dealer_id'=>$log['dealer_id'],
        'emp_id'=>$log['service_id'],
        'status'=>$log['result']==1 ? 2 : 3,
        'check_time'=>$log['createtime'],
        'remark'=>$log['remark'],
        'createtime'=>$log['createtime'],
    );
    $db->startTransaction();
    $order_data = array("check_car_status"=>$log['result']==1 ? 3 : 5);
    $ret = $db->insert('car_dealer_check',$data);
    $ret1 = $db->where('order_id',$log['order_id'])->update('order',$order_data);
    $db->commit();
    logUtil("order_id: ".$log['order_id']." 处理完成","INFO");
    $last_ocl_id = $log['ocl_id'];
}
logUtil("车商确认结果数据处理完成","INFO");
